package com.cms.user.perform;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.comm.StringFunction;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.DBManagerFactory;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.service.GeneralTools;
import net.vinote.smart.platform.service.HttpRequestPerform;


public class UserRegistPerform implements HttpRequestPerform {

	@Override
	public String perform(HttpServletRequest request) {
		String userID = StringFunction.nullToBlank(request
				.getParameter("userid"));
		String userName = StringFunction.nullToBlank(request
				.getParameter("username"));
		String password = StringFunction.nullToBlank(request
				.getParameter("password"));
		try {

			DBManager bm = DBManagerFactory.getInstance("user_info");
			BizObject bo = bm
					.createQuery(new String[] { "userid" }, "userid=?")
					.setParamVal(userID).getSingleResult();
			if (bo != null) {
				return "该账户已存在";
			}
			bo = new BizObject();
			bo.setAttribute("userID", userID);
			bo.setAttribute("userName", userName);
			bo.setAttribute(
					"password",
					GeneralTools.encrypt(password,
							GeneralTools.getAesKey(userID)));
			bm.saveBizObject(bo);
		} catch (SQLException e) {
			e.printStackTrace();
			return "Exception:" + e.getMessage();
		}
		return PlatformConst.SUCCESS;
	}

	@Override
	public String validityCheck(HttpServletRequest request) {
		String userID = StringFunction.nullToBlank(request
				.getParameter("userid"));
		String userName = StringFunction.nullToBlank(request
				.getParameter("username"));
		String password = StringFunction.nullToBlank(request
				.getParameter("password"));
		if ("".equals(userID)) {
			return "用户名不能为空";
		}
		if ("".equals(userName)) {
			return "姓名不能为空";
		}
		if ("".equals(password)) {
			return "密码不能为空";
		}
		return null;
	}

}
